package net.easyjava.base.service.impl;

import net.easyjava.base.Exception.ServiceException;
import net.easyjava.base.bean.ArticleBean;
import net.easyjava.base.bean.cache.ArticleCacheBean;
import net.easyjava.base.constants.RedisConstants;
import net.easyjava.base.dao.ArticleDao;
import net.easyjava.base.service.ArticleService;
import net.easyjava.base.utils.cache.RedisUtil;
import net.easyjava.tools.page.Page;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wenxiaoning on 15/12/18.
 */
@Component
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleDao articleDao;

    /**
     * 保存文章
     *
     * @param articleBean
     * @return
     */
    @Override
    public ArticleBean save(ArticleBean articleBean) throws ServiceException{
        return articleDao.save(articleBean)>0?find(articleBean.getId()):null;
    }



    /**
     * 查找一个篇文章
     *
     * @param id
     * @return
     */
    @Override
    public ArticleBean find(String id)throws ServiceException {
        return articleDao.findOne(id);
    }



    /**
     * 查找文章列表
     *
     * @param params
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<ArticleBean> findList(Map<String, Object> params, int pageNum, int pageSize)throws ServiceException {
        params.put("start",(pageNum-1)*pageSize);
        params.put("pageSize",pageSize);
        List<ArticleBean> list = articleDao.findList(params);
        int count = articleDao.findCount(params);
        return new Page<ArticleBean>(list,count,pageNum);
    }

    @Override
    public Page<ArticleBean> findAllList() {
        Map<String, Object> params  = new HashMap<String, Object>();
        params.put("start",0);
        params.put("pageSize",Long.MAX_VALUE);
        List<ArticleBean> list = articleDao.findList(params);
        int count = articleDao.findCount(params);
        return new Page<ArticleBean>(list,count,1);
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(String id)throws ServiceException {
        return articleDao.delete(id)>0?true:false;
    }

    /**
     * 修改文章
     *
     * @param articleBean
     * @return
     */
    @Override
    public ArticleBean update(ArticleBean articleBean)throws ServiceException {
        return articleDao.update(articleBean)>0?find(articleBean.getId()):null;
    }


    /**
     * 保存缓存类
     *
     * @param articleCacheBean
     * @return
     * @throws ServiceException
     */
    @Override
    public void saveCache(ArticleCacheBean articleCacheBean) throws ServiceException {
        //单个文章的缓存
        String key = RedisConstants.KEY_ARTICLE_ONE+articleCacheBean.getId();
        RedisUtil.remove(key);
        RedisUtil.set(key,articleCacheBean);
        //按照专辑分列表
        String albumKey = RedisConstants.KEY_ARTICLE_LIST_ALBUM+articleCacheBean.getAlbumId();
        RedisUtil.ldel(albumKey,articleCacheBean.getId());
        RedisUtil.lpush(albumKey, articleCacheBean.getId());
        //总列表
        String listKey = RedisConstants.KEY_ARTICLE_LIST;
        RedisUtil.ldel(listKey,articleCacheBean.getId());
        RedisUtil.lpush(listKey, articleCacheBean.getId());
    }

    /**
     * 查找缓存类
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    public ArticleCacheBean findCache(String id) throws ServiceException {
        return (ArticleCacheBean) RedisUtil.get(RedisConstants.KEY_ARTICLE_ONE+id);
    }




    /**
     * 删除缓存类
     *
     * @param id
     * @throws ServiceException
     */
    @Override
    public void deleteCache(String id) throws ServiceException {
        RedisUtil.remove(RedisConstants.KEY_ARTICLE_ONE+id);
    }

    /**
     * 清空缓存
     */
    @Override
    public void removeCache() {
        String[] oneKeys = RedisUtil.getArrayKeys(RedisConstants.KEY_ARTICLE_ONE+"*");
        RedisUtil.removeByKeys(oneKeys);
        String[] albumKeys = RedisUtil.getArrayKeys(RedisConstants.KEY_ARTICLE_LIST_ALBUM+"*");
        RedisUtil.removeByKeys(albumKeys);
        RedisUtil.remove(RedisConstants.KEY_ARTICLE_LIST);
    }


    /**
     * 获取全部类型列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<ArticleCacheBean> findCacheAllList(int pageNum, int pageSize) {
        String key = RedisConstants.KEY_ARTICLE_LIST;
        List<String> totalList = RedisUtil.lrange(key,0l,Long.MAX_VALUE);
        long start = (pageNum-1)*pageSize;
        long end = start+pageSize;
        List<String> ids = RedisUtil.lrange(key,start,end);
        List<ArticleCacheBean> list = new ArrayList<ArticleCacheBean>();
        for(String id:ids){
            ArticleCacheBean articleCacheBean = findCache(id);
            list.add(articleCacheBean);
        }
        return new Page<ArticleCacheBean>(list,totalList.size(),pageNum);
    }

    /**
     * 根据albumId获取列表
     *
     * @param albumId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<ArticleCacheBean> findCacheList(int albumId, int pageNum, int pageSize) {
        String key = RedisConstants.KEY_ARTICLE_LIST_ALBUM+albumId;
        List<String> totalList = RedisUtil.lrange(key,0l,Long.MAX_VALUE);
        long start = (pageNum-1)*pageSize;
        long end = start+pageSize;
        List<String> ids = RedisUtil.lrange(key,start,end);
        List<ArticleCacheBean> list = new ArrayList<ArticleCacheBean>();
        for(String id:ids){
            ArticleCacheBean articleCacheBean = findCache(id);
            list.add(articleCacheBean);
        }
        return new Page<ArticleCacheBean>(list,totalList.size(),pageNum);
    }
}
